<?php

class MyLotto_3d
{

    public $disk_type = array('a' => "a", 'b' => "b", 'c' => "c", 'd' => "d");
    public $lotto_3d_id;
    public $gametype;
    public $table_name = "lotto_3d";
    public $db;
    public $tbl;
    public $_8NFlag = FALSE;
    public $openUrl = array(
        0 => "",
        1 => "",
        2 => "",
        10 => "",
        11 => "",
        12 => "",
        13 => "",
        14 => "",
        15 => "",
        20 => "",
        21 => "",
        22 => "",
        23 => "",
		24 => "",
		25 => "",
		26 => ""
    );
    public $BNLeng = array(0, 0, 0, 5, 9, 9, 9, 9, 9, 14, 23, 23, 23);

    public function MyLotto_3d($gametype = 1)
    {
        global $g_gametype_3d;
        $this->table_name = "lotto_" . $g_gametype_3d[$gametype]['en'];
        $this->tbl = $g_gametype_3d[$gametype]['en'];
        $this->db = new DB();
        $this->gametype = $gametype;
        if (substr($g_gametype_3d[$gametype]['en'], 0, 2) == "8h")
        {
            $this->_8NFlag = TRUE;
        }
    }

    public function getOpenUrl($gametype)
    {
        return $this->openUrl[$gametype];
    }

    public function getBNLeng($gametype)
    {
        return $this->BNLeng[$gametype];
    }

    public function setLotto_3d_id($lotto_3d_id)
    {
        $this->lotto_3d_id = $lotto_3d_id;
    }

    public function getDataList($page, $pagesize = 20)
    {
        $rs = new RecordSet();
        $sql = "SELECT * FROM {$this->table_name} WHERE open != '0000-00-00' ORDER BY open DESC,round DESC";
        $rs->open($sql, $page, $pagesize);
        return $rs;
    }

    public function getDataList3s($page, $pagesize = 20)
    {
        $rs = new RecordSet();
        $sql = "SELECT * FROM {$this->table_name} WHERE '" . date("Y-m-d H:i:s") . "' >= startbet AND open != '0000-00-00' ORDER BY open DESC,openbet DESC";
		$rs->open($sql, $page, $pagesize);
        return $rs;
    }

    public function getLotto_3d_id($chkToday = FALSE)
    {
        $todays = date("Y-m-d", strtotime("-1 day"));
        $todays_c = date("Y-m-d");
        switch ($this->gametype)
        {
            case 12 :
                if (date("Hi") < "0210")
                {
                    $paperDay = date("Y-m-d", strtotime("-2 day"));
                    $paperDay_c = date("Y-m-d", strtotime("-1 day"));
                }
                else
                {
                    $paperDay = $todays;
                    $paperDay_c = $todays_c;
                }
                break;
            case 14 :
                if (date("Hi") < "0210")
                {
                    $paperDay = date("Y-m-d", strtotime("-2 day"));
                    $paperDay_c = date("Y-m-d", strtotime("-1 day"));
                }
                else
                {
                    $paperDay = $todays;
                    $paperDay_c = $todays_c;
                }
                break;
            default :
                $paperDay = $todays;
                $paperDay_c = $todays_c;
                break;
        }
        if ($chkToday)
        {
            $sql = "!!SELECT round FROM {$this->table_name} WHERE open = '{$paperDay_c}' ORDER BY round ASC LIMIT 1";
            if ($result = $this->db->query($sql))
            {
                $row = mysql_fetch_row($result);
                if ($row[0] != 0)
                {
                    if ($this->gametype == 20)
                    {
                        return str_pad(intval(substr($row[0], 0, 7)) - 1, 3, "0", STR_PAD_LEFT) . "-01";
                    }
                    else
                    {
                        return $row[0] - 1;
                    }
                }
            }
        }
        $sql = "SELECT max(round) FROM {$this->table_name} WHERE open = '{$paperDay}'";
        if ($result = $this->db->query($sql))
        {
            $row = mysql_fetch_row($result);
            if ($row[0] != 0)
            {
                return $row[0];
            }
            else
            {
                $sql = "SELECT round FROM {$this->table_name} WHERE (open between '" . date("Y") . "-01-01' and '{$todays}') ORDER BY open DESC, id DESC LIMIT 1";
                $result = $this->db->query($sql);
                if ($row = mysql_fetch_row($result))
                {
                    return $row[0];
                }
                else
                {
                    return FALSE;
                }
                return FALSE;
            }
        }
        return FALSE;
    }

    public function getDataById($id)
    {
        $sql = "!!SELECT * FROM {$this->table_name} WHERE id={$id} ORDER BY round DESC LIMIT 0,1";
        $result = $this->db->query($sql);
        return mysql_fetch_array($result, MYSQL_ASSOC);
    }

    public function getDataBeforeDate($today, $num, $show3D3S = 0, $page = 0)
    {
        $rs = new RecordSet();
        switch ($show3D3S)
        {
            case 0 :
                $sql = "SELECT * FROM {$this->table_name} WHERE open<= '{$today}' && open!= '0000-00-00' ORDER BY open DESC LIMIT 0,{$num}";
                break;
            case 1 :
                $sql = "SELECT * FROM {$this->table_name} WHERE startbet <= '" . date("Y-m-d H:i:s") . "' && open!= '0000-00-00' ORDER BY openbet DESC,round DESC LIMIT 0,{$num}";
                break;
            case 2 :
                $sql = "SELECT * FROM {$this->table_name} WHERE startbet <= '" . date("Y-m-d H:i:s") . "' && open!= '0000-00-00' ORDER BY openbet DESC,round DESC LIMIT 0,{$num}";
                break;
            default :
                return FALSE;
        }
        $rs->open($sql, $page, 20);
        return $rs;
    }

    public function getDataBeforeOpen($today, $open, $show3D3S = 0, $page = 0)
    {
        $rs = new RecordSet();
        switch ($show3D3S)
        {
            case 0 :
                $sql = "SELECT * FROM {$this->table_name} WHERE open<= '{$today}' && open >= '{$open}' ORDER BY open DESC LIMIT 90";
                break;
            case 1 :
                $sql = "SELECT * FROM {$this->table_name} WHERE startbet <= '" . date("Y-m-d H:i:s") . "' && open >= '{$open}' ORDER BY openbet DESC,round DESC LIMIT 90";
                break;
            case 2 :
                $sql = "SELECT * FROM {$this->table_name} WHERE startbet <= '" . date("Y-m-d H:i:s") . "' && open >= '{$open}' ORDER BY openbet DESC,round DESC LIMIT 90";
                break;
            default :
                return FALSE;
        }
        $rs->open($sql, $page, 20);
        return $rs;
    }

    public function getDataByRound($round)
    {
        $sql = "SELECT * FROM {$this->table_name} WHERE round='{$round}' ORDER BY round DESC LIMIT 1";
        $result = $this->db->query($sql);
        return mysql_fetch_array($result, MYSQL_ASSOC);
    }

    public function getLotto3DByDate($day)
    {
        $sql = "SELECT * FROM {$this->table_name} WHERE open = '{$day}' ORDER BY open limit 1";
        $result = $this->db->query($sql);
        return mysql_fetch_array($result, MYSQL_ASSOC);
    }

    public function getLotto3SByDateTime()
    {
        $sql = "SELECT * FROM {$this->table_name} WHERE ('" . date("Y-m-d H:i:s", systime()) . "' BETWEEN startbet AND stopbet) AND ((n3 + n2 + n1) = -3) limit 1";
        $result = $this->db->query($sql);
        return mysql_fetch_array($result, MYSQL_ASSOC);
    }

    /* 下一期时间 */

    public function getLottoNextDateTime()
    {
        $n = "";
        if ($this->gametype == 10)
        {
            $n = "((n3 + n2 + n1) = -3)";
        }
        else
        {
            $n = "((n5 + n4 + n3 + n2 + n1) = -5)";
        }
        $sql = "SELECT time_to_sec(timediff(startbet,sysdate())) as sec FROM {$this->table_name} WHERE startbet > sysdate() AND {$n} ORDER BY startbet ASC limit 0,1";
        $result = $this->db->query($sql);
        unset($sql);
        return mysql_fetch_array($result, MYSQL_ASSOC);
    }

    public function getLotto3DLastDate($day)
    {
        $sql = "SELECT * FROM {$this->table_name} WHERE open >= '{$day}' ORDER BY open limit 0,1";
        $result = $this->db->query($sql);
        return mysql_fetch_array($result, MYSQL_ASSOC);
    }

    public function getLotto3SLastDate()
    {
        if ($this->gametype == 24)
        {
            $sql = "SELECT * FROM {$this->table_name} WHERE '" . date("Y-m-d H:i:s") . "' BETWEEN startbet AND stopbet limit 0,1";
        }
        else
        {
            $sql = "SELECT * FROM {$this->table_name} WHERE '" . date("Y-m-d H:i:s") . "' BETWEEN startbet AND openbet limit 0,1";
        }
        $result = $this->db->query($sql);
        return mysql_fetch_array($result, MYSQL_ASSOC);
    }

    public function checkRoundOpen($round, $open)
    {
        $sql = "SELECT id FROM {$this->table_name} WHERE open='{$open}' ORDER BY round DESC LIMIT 0,1";
        $result = $this->db->query($sql);
        if (mysql_fetch_array($result))
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }

    public function checkRoundOpen3S($round, $open)
    {
        $sql = "SELECT id FROM {$this->table_name} WHERE open='{$open}' AND round='{$round}' ORDER BY round DESC LIMIT 0,1";
        $result = $this->db->query($sql);
        if (mysql_fetch_array($result))
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }

    public function checkRoundOpenBet3S($openbet)
    {
        $sql = "SELECT id FROM {$this->table_name} WHERE openbet='{$openbet}' and open != '0000-00-00' ORDER BY round DESC LIMIT 0,1";
        $result = $this->db->query($sql);
        if (mysql_fetch_array($result))
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }

    public function add($dataArr)
    {
        if (isset($dataArr['id']))
        {
            unset($dataArr['id']);
        }
        $sql = "INSERT INTO {$this->table_name} ";
        $sql .= "(" . implode(",", array_keys($dataArr)) . ")";
        $sql .= " VALUES('" . implode("','", $dataArr) . "')";
        if (!$this->db->query($sql))
        {
            return FALSE;
        }
        else
        {
            return mysql_insert_id($this->db->handle_master);
        }
    }

    public function update($dataArr, $id)
    {
        if (isset($dataArr['id']))
        {
            unset($dataArr['id']);
        }
        if (isset($dataArr['open']) && $dataArr['open'] == "0000-00-00")
        {
            $sql = "DELETE FROM {$this->table_name} WHERE open = '0000-00-00' ";
            $this->db->query($sql);
        }
        $sql = " UPDATE {$this->table_name} SET ";
        foreach ($dataArr as $key => $val)
        {
            $sql .= $key . " = '" . $val . "', ";
        }
        if (substr($sql, 0 - 2) == ", ")
        {
            $sql = substr($sql, 0, strlen($sql) - 2);
        }
        $sql .= " WHERE id=" . $id;
        $this->db->query($sql);
    }

    public function _copyRateZeroToN($lotto_3d_id)
    {
        $table_name = "ratea_{$this->tbl}";
        $sql = "INSERT INTO " . $table_name . " (lotto_id, bettype, subid, ratedata, updatetime)";
        $sql .= " SELECT '" . $lotto_3d_id . "' AS lotto_id, bettype, subid, ratedata, '" . systime() . "' as updatetime";
        $sql .= " FROM " . $table_name . " WHERE lotto_id=0";
        $this->db->query($sql);
        $table_name = "rateb_{$this->tbl}";
        $sql = "INSERT INTO " . $table_name . " (lotto_id, bettype, subid, ratedata, updatetime)";
        $sql .= " SELECT '" . $lotto_3d_id . "' AS lotto_id, bettype, subid, ratedata, '" . systime() . "' as updatetime";
        $sql .= " FROM " . $table_name . " WHERE lotto_id=0";
        $this->db->query($sql);
        $table_name = "ratec_{$this->tbl}";
        $sql = "INSERT INTO " . $table_name . " (lotto_id, bettype, subid, ratedata, updatetime)";
        $sql .= " SELECT " . $lotto_3d_id . " AS lotto_id, bettype, subid, ratedata, '" . systime() . "' as updatetime";
        $sql .= " FROM " . $table_name . " WHERE lotto_id=0";
        $this->db->query($sql);
        $table_name = "rated_{$this->tbl}";
        $sql = "INSERT INTO " . $table_name . " (lotto_id, bettype, subid, ratedata, updatetime)";
        $sql .= " SELECT " . $lotto_3d_id . " AS lotto_id, bettype, subid, ratedata, '" . systime() . "' as updatetime";
        $sql .= " FROM " . $table_name . " WHERE lotto_id=0";
        return $this->db->query($sql);
    }

    public function copyRateZeroToN($lotto_3d_id)
    {
        $table_name = "rate_{$this->tbl}";
        $sql = "INSERT INTO {$table_name} (`lotto_id`, `bettype`, `subid`, `disk`, `r00`, `r01`, `r02`, `r03`, `r04`, `r05`, `r06`, `r07`, `r08`, `r09`, `r10`, `r11`, `r12`, `r13`, `r14`, `r15`, `r16`, `r17`, `r18`, `r19`, `r20`, `r21`)";
        $sql .= " SELECT '{$lotto_3d_id}' AS lotto_id, `bettype`, `subid`, `disk`, `r00`, `r01`, `r02`, `r03`, `r04`, `r05`, `r06`, `r07`, `r08`, `r09`, `r10`, `r11`, `r12`, `r13`, `r14`, `r15`, `r16`, `r17`, `r18`, `r19`, `r20`, `r21`";
        $sql .= " FROM {$table_name} WHERE lotto_id=0";
        return $this->db->query($sql);
    }

    public function createTblBettotal($lotto_3d_id)
    {
        global $g_gametype_3d;
        $table_name = "bettotal_{$this->tbl}_" . $lotto_3d_id;
        $xyidFlag = $g_gametype_3d[$this->gametype]['type'] == 3 ? 1 : 0;
        $rateFloatType = "10,3";
        $sql = "CREATE TABLE IF NOT EXISTS {$table_name} ( ";
        $sql .= "id int(6) NOT NULL auto_increment COMMENT 'ID', ";
        $sql .= "lotto_id int(6) NOT NULL default '0' COMMENT '期数', ";
        $sql .= "user_id int(6) NOT NULL default '0' COMMENT '使用者id', ";
        $sql .= "bettype int(2) default NULL COMMENT '下注类别', ";
        if ($xyidFlag)
        {
            $sql .= "xyid tinyint (1) NOT NULL default '1', ";
        }
        $sql .= "subid int(1) default '0' COMMENT '前中後', ";
        $sql .= "numbers varchar(3) NOT NULL COMMENT '下注内容', ";
        $sql .= "totalnum int(7) default NULL COMMENT '笔数', ";
        $sql .= "amount1 float(14,3) NOT NULL default '0' COMMENT '下注累计金额不计算占成', ";
        $sql .= "amount2 float(14,3) NOT NULL default '0' COMMENT '下注累计金额计算占成', ";
        $sql .= "amount3 float(14,3) NOT NULL default '0' COMMENT '累计输赢金额(下注额*赔率)', ";
        $sql .= "amount4 float(14,3) NOT NULL default '0' COMMENT '累计输赢(占成额*赔率)', ";
        $sql .= "back1 float(9,3) NOT NULL default '0.000' COMMENT '注单总退水', ";
        $sql .= "back2 float(9,3) NOT NULL default '0.000' COMMENT '管理层退水', ";
        $sql .= "back3 float(9,3) NOT NULL default '0.000' COMMENT '管理层退水(计占成)', ";
        $sql .= "rate1 float({$rateFloatType}) NOT NULL default '0.00' COMMENT '平均赔率(依笔数)', ";
        $sql .= "rate2 float({$rateFloatType}) NOT NULL default '0.00' COMMENT '输赢赔率', ";
        $sql .= "totalnuma int(7) default NULL COMMENT '笔数', ";
        $sql .= "amount1a float(14,3) NOT NULL default '0' COMMENT '下注累计金额不计算占成', ";
        $sql .= "amount2a float(14,3) NOT NULL default '0' COMMENT '下注累计金额计算占成', ";
        $sql .= "amount3a float(14,3) NOT NULL default '0' COMMENT '累计输赢金额(下注额*赔率)', ";
        $sql .= "amount4a float(14,3) default '0' COMMENT '累计输赢(占成额*赔率)', ";
        $sql .= "back1a float(9,3) NOT NULL default '0.000' COMMENT '注单总退水', ";
        $sql .= "back2a float(9,3) NOT NULL default '0.000' COMMENT '管理层退水', ";
        $sql .= "back3a float(9,3) NOT NULL default '0.000' COMMENT '管理层退水(计占成)', ";
        $sql .= "rate1a float({$rateFloatType}) NOT NULL default '0.00' COMMENT '平均赔率(依笔数)', ";
        $sql .= "rate2a float({$rateFloatType}) NOT NULL default '0.00' COMMENT '输赢赔率', ";
        $sql .= "totalnumb int(7) default NULL COMMENT '笔数', ";
        $sql .= "amount1b float(14,3) NOT NULL default '0' COMMENT '下注累计金额不计算占成', ";
        $sql .= "amount2b float(14,3) NOT NULL default '0' COMMENT '下注累计金额计算占成', ";
        $sql .= "amount3b float(14,3) NOT NULL default '0' COMMENT '累计输赢金额(下注额*赔率)', ";
        $sql .= "amount4b float(14,3) default '0' COMMENT '累计输赢(占成额*赔率)', ";
        $sql .= "back1b float(9,3) NOT NULL default '0.000' COMMENT '注单总退水', ";
        $sql .= "back2b float(9,3) NOT NULL default '0.000' COMMENT '管理层退水', ";
        $sql .= "back3b float(9,3) NOT NULL default '0.000' COMMENT '管理层退水(计占成)', ";
        $sql .= "rate1b float({$rateFloatType}) NOT NULL default '0.00' COMMENT '平均赔率(依笔数)', ";
        $sql .= "rate2b float({$rateFloatType}) NOT NULL default '0.00' COMMENT '输赢赔率', ";
        $sql .= "totalnumc int(7) default NULL COMMENT '笔数', ";
        $sql .= "amount1c float(14,3) NOT NULL default '0' COMMENT '下注累计金额不计算占成', ";
        $sql .= "amount2c float(14,3) NOT NULL default '0' COMMENT '下注累计金额计算占成', ";
        $sql .= "amount3c float(14,3) NOT NULL default '0' COMMENT '累计输赢金额(下注额*赔率)', ";
        $sql .= "amount4c float(14,3) default '0' COMMENT '累计输赢(占成额*赔率)', ";
        $sql .= "back1c float(9,3) NOT NULL default '0.000' COMMENT '注单总退水', ";
        $sql .= "back2c float(9,3) NOT NULL default '0.000' COMMENT '管理层退水', ";
        $sql .= "back3c float(9,3) NOT NULL default '0.000' COMMENT '管理层退水(计占成)', ";
        $sql .= "rate1c float({$rateFloatType}) NOT NULL default '0.00' COMMENT '平均赔率(依笔数)', ";
        $sql .= "rate2c float({$rateFloatType}) NOT NULL default '0.00' COMMENT '输赢赔率', ";
        $sql .= "totalnumd int(7) default NULL COMMENT '笔数', ";
        $sql .= "amount1d float(14,3) NOT NULL default '0' COMMENT '下注累计金额不计算占成', ";
        $sql .= "amount2d float(14,3) NOT NULL default '0' COMMENT '下注累计金额计算占成', ";
        $sql .= "amount3d float(14,3) NOT NULL default '0' COMMENT '累计输赢金额(下注额*赔率)', ";
        $sql .= "amount4d float(14,3) default '0' COMMENT '累计输赢(占成额*赔率)', ";
        $sql .= "back1d float(9,3) NOT NULL default '0.000' COMMENT '注单总退水', ";
        $sql .= "back2d float(9,3) NOT NULL default '0.000' COMMENT '管理层退水', ";
        $sql .= "back3d float(9,3) NOT NULL default '0.000' COMMENT '管理层退水(计占成)', ";
        $sql .= "rate1d float({$rateFloatType}) NOT NULL default '0.00' COMMENT '平均赔率(依笔数)', ";
        $sql .= "rate2d float({$rateFloatType}) NOT NULL default '0.00' COMMENT '输赢赔率', ";
        $sql .= "updatetime int(11) default '0' COMMENT '更新时间', ";
        $sql .= "PRIMARY KEY  (id), ";
        if ($xyidFlag)
        {
            $sql .= "UNIQUE KEY lotto_id (lotto_id,user_id,bettype,xyid,subid,numbers) ";
        }
        else
        {
            $sql .= "UNIQUE KEY lotto_id (lotto_id,user_id,bettype,subid,numbers) ";
        }
        $sql .= ") ENGINE=MyISAM;";
        return $this->db->query($sql);
    }

    public function getOpenInfo($gametype, $round, $getOpen = FALSE)
    {
        $conf = new Config();
        $reArr = array();
        switch ($gametype)
        {
            case 10 :
                $preClose = max(intval($conf->get("preClose_3s1")), 3) * 60;
                $reArr[] = array("start" => "10:30:00", "next" => "1800", "preClose" => $preClose, "round" => "23", "sn" => "0", "preStr" => "", "len" => "2", "startPre" => "0");
                break;
            case 11 :
                if ($getOpen)
                {
                    $datemd = date("Ymd", $getOpen) . "-";
                }
                else
                {
                    $datemd = date("Ymd") . "-";
                }
                $preClose = max(intval($conf->get("preClose_3s2")), 1) * 60;
                $reArr[] = array("start" => "09:10:00", "next" => "600", "preClose" => $preClose, "round" => "84", "sn" => "0", "preStr" => $datemd, "len" => "3", "startPre" => "0");
                break;
            case 12 :
                $datemd = $getOpen ? date("md", $getOpen) : date("md");
                $preClose = max(intval($conf->get("preClose_3s3")), 1) * 60;
                $reArr[] = array("start" => "10:00:00", "next" => "600", "preClose" => $preClose, "round" => "72", "sn" => "23", "preStr" => $datemd, "len" => "3", "startPre" => "0");
                $reArr[] = array("start" => "22:00:00", "next" => "300", "preClose" => $preClose, "round" => "48", "sn" => "23", "preStr" => $datemd, "len" => "3", "startPre" => "0");
                break;
            case 13 :
                $datemd = $getOpen ? date("md", $getOpen) : date("md");
                $preClose = max(intval($conf->get("preClose_3s4")), 1) * 60;
                $reArr[] = array("start" => "09:10:00", "next" => "600", "preClose" => $preClose, "round" => "6", "sn" => "0", "preStr" => $datemd, "len" => "3", "startPre" => "0");
                $reArr[] = array("start" => "10:11:00", "next" => "600", "preClose" => $preClose, "round" => "6", "sn" => "0", "preStr" => $datemd, "len" => "3", "startPre" => "0");
                $reArr[] = array("start" => "11:12:00", "next" => "600", "preClose" => $preClose, "round" => "6", "sn" => "0", "preStr" => $datemd, "len" => "3", "startPre" => "0");
                $reArr[] = array("start" => "12:13:00", "next" => "600", "preClose" => $preClose, "round" => "6", "sn" => "0", "preStr" => $datemd, "len" => "3", "startPre" => "0");
                $reArr[] = array("start" => "13:14:00", "next" => "600", "preClose" => $preClose, "round" => "6", "sn" => "0", "preStr" => $datemd, "len" => "3", "startPre" => "0");
                $reArr[] = array("start" => "14:15:00", "next" => "600", "preClose" => $preClose, "round" => "6", "sn" => "0", "preStr" => $datemd, "len" => "3", "startPre" => "0");
                $reArr[] = array("start" => "15:16:00", "next" => "600", "preClose" => $preClose, "round" => "6", "sn" => "0", "preStr" => $datemd, "len" => "3", "startPre" => "0");
                $reArr[] = array("start" => "16:17:00", "next" => "600", "preClose" => $preClose, "round" => "6", "sn" => "0", "preStr" => $datemd, "len" => "3", "startPre" => "0");
                $reArr[] = array("start" => "17:18:00", "next" => "600", "preClose" => $preClose, "round" => "6", "sn" => "0", "preStr" => $datemd, "len" => "3", "startPre" => "0");
                $reArr[] = array("start" => "18:19:00", "next" => "600", "preClose" => $preClose, "round" => "6", "sn" => "0", "preStr" => $datemd, "len" => "3", "startPre" => "0");
                $reArr[] = array("start" => "19:20:00", "next" => "600", "preClose" => $preClose, "round" => "6", "sn" => "0", "preStr" => $datemd, "len" => "3", "startPre" => "0");
                $reArr[] = array("start" => "20:21:00", "next" => "600", "preClose" => $preClose, "round" => "6", "sn" => "0", "preStr" => $datemd, "len" => "3", "startPre" => "0");
                $reArr[] = array("start" => "21:22:00", "next" => "600", "preClose" => $preClose, "round" => "6", "sn" => "0", "preStr" => $datemd, "len" => "3", "startPre" => "0");
                $reArr[] = array("start" => "22:23:00", "next" => "600", "preClose" => $preClose, "round" => "6", "sn" => "0", "preStr" => $datemd, "len" => "3", "startPre" => "0");
                break;
            case 14 :
                $datemd = $getOpen ? date("md", $getOpen) : date("md");
                $preClose = max(intval($conf->get("preClose_3s6")), 1) * 60;
                $reArr[] = array("start" => "10:10:00", "next" => "600", "preClose" => $preClose, "round" => "96", "sn" => "0", "preStr" => $datemd, "len" => "3", "startPre" => "0");
                break;
            case 15 :
                $datemd = $getOpen ? date("md", $getOpen) : date("md");
                $preClose = max(intval($conf->get("preClose_3s" . ( $gametype == 14 ) ? "5" : "6" )), 1) * 60;
                $reArr[] = array("start" => $gametype == 14 ? "12:10:00" : "09:10:00", "next" => "600", "preClose" => $preClose, "round" => $gametype == 14 ? "84" : "88", "sn" => "0", "preStr" => $datemd, "len" => "3", "startPre" => "0");
                break;
            case 20 :
                if ($round == 0)
                {
                    $round = "0000000-01";
                }
                if ($getOpen)
                {
                    $chkStr = date("m", $getOpen) . date("d", $getOpen);
                }
                else
                {
                    $chkStr = date("m") . date("d");
                }
                if ($chkStr != "0101")
                {
                    $roundThree = str_pad(intval(substr($round, 4, 3)) + 1, 3, "0", STR_PAD_LEFT);
                }
                else
                {
                    $roundThree = "001";
                }
                if ($getOpen)
                {
                    $datemd = date("Y", $getOpen) . $roundThree . "-";
                }
                else
                {
                    $datemd = date("Y") . $roundThree . "-";
                }
                $preClose = max(intval($conf->get("preClose_5h")), 3) * 60;
                $reArr[] = array("start" => "09:15:00", "next" => "900", "preClose" => $preClose, "round" => "50", "sn" => "0", "preStr" => $datemd, "len" => "2", "startPre" => "0");
                break;
            case 21 :
                if ($getOpen)
                {
                    $datemd = date("Ymd", $getOpen) . "-";
                }
                else
                {
                    $datemd = date("Ymd") . "-";
                }
                $preClose = max(intval($conf->get("preClose_8h" . ( $gametype == 21 ) ? "" : "3" )), 1) * 60;
                $reArr[] = array("start" => "08:40:00", "next" => "600", "preClose" => $preClose, "round" => "84", "sn" => "0", "preStr" => $datemd, "len" => "2", "startPre" => "0");
                break;
            case 23 :
                if ($getOpen)
                {
                    $datemd = date("Ymd", $getOpen) . "-";
                }
                else
                {
                    $datemd = date("Ymd") . "-";
                }
                $preClose = max(intval($conf->get("preClose_8h" . ( $gametype == 21 ) ? "" : "3" )), 1) * 60;
               	$reArr[] = array("start" => "10:03:00", "next" => "600", "preClose" => $preClose, "round" => "97", "sn" => "13", "preStr" => $datemd, "len" => "2", "startPre" => "0");
				break;
            case 22 :
                if ($getOpen)
                {
                    $datemd = date("Ymd", $getOpen) . "-";
                }
                else
                {
                    $datemd = date("Ymd") . "-";
                }
                $preClose = max(intval($conf->get("preClose_8h2")), 1) * 60;
                $reArr[0] = array("start" => "09:06:00", "next" => "600", "preClose" => $preClose, "round" => "15", "sn" => "0", "preStr" => $datemd, "len" => "2", "startPre" => "0");
                $i = 1;
                for (; $i < 6; $i++)
                {
                    $start_time = 150 * $i + $i;
                    $reArr[$i] = array("start" => date("H:i:s", strtotime("+ {$start_time} minutes", strtotime("09:06:00"))), "next" => "600", "preClose" => $preClose, "round" => $i == 5 ? "9" : "15", "sn" => "0", "preStr" => $datemd, "len" => "2", "startPre" => "0");
                }
                break;
            case 24 :
                $preClose = max(intval($conf->get("preClose_pk10")), 0) * 60;
                if ($round == 0)
                {
                    $nowqs = 1;
                }
                else
                {
                    $nowqs = $round + 1;
                }
                $reArr[0] = array("start" => "09:07:00", "next" => "300", "preClose" => $preClose, "round" => "11", "sn" => "0", "preStr" => $nowqs, "len" => "0", "startPre" => "0");
                $nowtime = strtotime('09:02:00');
                for ($i = 1; $i < 15; $i++)
                {
                    $nowtime = $nowtime + 3600;
                    $reArr[$i] = array("start" => date("H:i:s", $nowtime), "next" => "300", "preClose" => $preClose, "round" => "12", "sn" => "0", "preStr" => $nowqs + 12, "len" => "0", "startPre" => "0");
                }
                break;
			case 25 :
				if ($getOpen)
                {
                    $datemd = date("Ymd", $getOpen) . "-";
                }
                else
                {
                    $datemd = date("Ymd") . "-";
                }
                $preClose = max(intval($conf->get("preClose_sb")), 1) * 60;
                $reArr[] = array("start" => "08:40:00", "next" => "600", "preClose" => $preClose, "round" => "82", "sn" => "0", "preStr" => $datemd, "len" => "2", "startPre" => "0");
                break;
			case 26 :
                if ($getOpen)
                {
                    $datemd = date("Ymd", $getOpen) . "-";
                }
                else
                {
                    $datemd = date("Ymd") . "-";
                }
                $preClose = max(intval($conf->get("preClose_sb")), 1) * 60;
                $reArr[] = array("start" => "08:50:00", "next" => "600", "preClose" => $preClose, "round" => "80", "sn" => "0", "preStr" => $datemd, "len" => "2", "startPre" => "0");
                break;
        }
        return $reArr;
    }

    public function getBetTime($autoLottoTime = -1)
    {
        if ($autoLottoTime < 0)
        {
            $autoLottoTime = time();
        }
        $reArr = array(array(), array());
        $open = date("Hi", $autoLottoTime) < "0210" ? date("Y-m-d", $autoLottoTime - 86400) : date("Y-m-d", $autoLottoTime);
        $maxRound = 0;
        if ($this->gametype == 11 || $this->gametype == 20 || $this->gametype == 24)
        {
            $maxRound = $this->getLotto_3d_id(TRUE);
            if (!$maxRound)
            {
                $maxRound = 0;
            }
        }
        $openInfoArr = $this->getOpenInfo($this->gametype, $maxRound, strtotime($open));
        $tempI = 0;
        $openTimeInt = 0;
        $startTimeInt = 0;
        $cutI = 0;
        foreach ($openInfoArr as $showListVal)
        {
            $openTimeInt = $openTimeInt == 0 ? strtotime($open . " " . $showListVal['start']) : $openTimeInt;
            $startTimeInt = $startTimeInt == 0 ? $openTimeInt - $showListVal['next'] : $startTimeInt;
            for ($i = 0; $i < $showListVal['round']; $i++)
            {
                $tempI++;
                $startTimeInt = $startTimeInt + $showListVal['startPre'];
                $startDateHM = date("Y-m-d H:i:00", $startTimeInt);
                $openDateHM = date("Y-m-d H:i:00", $openTimeInt);
                $closeTimeInt = $openTimeInt - $showListVal['preClose'];
                $closeDateHM = date("Y-m-d H:i:00", $closeTimeInt);
                if (date("Hi", $startTimeInt) == "0000")
                {
                    $cutI = $this->gametype == 12 ? 120 : 0;
                    $showListVal['preStr'] = $this->gametype == 14 ? date("md", strtotime($open)) : date("md", strtotime($open) + 86400);
                }
				else if (date("Hi", $startTimeInt) == "2353"  && $this->gametype==23)
                {
                    $cutI = 97;
                    $showListVal['preStr'] = date("Ymd" , $startTimeInt+24*3600).'-';
                }
                if ($this->gametype == 24)
                {
                    if ($tempI == 1)
                    {
                        $roundStr = $showListVal['preStr'];
                    }
                    else
                    {
                        $roundStr = $roundStr + 1;
                    }
                }
                else
                {
                    $roundStr = $showListVal['preStr'] . str_pad($showListVal['sn'] + $tempI - $cutI, $showListVal['len'], "0", STR_PAD_LEFT);
                }
                $tempIdx = $tempI - 1;
                $reArr[0][$tempIdx] = $startDateHM;
                $reArr[1][$tempIdx] = array();
                $reArr[1][$tempIdx][0] = $roundStr;
                $reArr[1][$tempIdx][1] = $closeDateHM;
                $reArr[1][$tempIdx][2] = $openDateHM;
                $reArr[1][$tempIdx][3] = $open;
                $startTimeInt = $openTimeInt;
                $openTimeInt += $showListVal['next'];
            }
            $openTimeInt = 0;
        }
        return $reArr;
    }

    public function autoInserLotto($autoLottoTime = -1)
    {
        global $g_gametype_3d;
        $conf = new Config();
        $cnyDateSInt = strtotime($conf->get("cnyDateS") . " 00:00:00");
        $cnyDateEInt = strtotime($conf->get("cnyDateE") . " 23:59:59");
        if ($autoLottoTime < 0)
        {
            $autoLottoTime = time();
        }
        $betTimeArr = $this->getBetTime($autoLottoTime);
        $betTimeArr01 = $betTimeArr[0];
        $betTimeArr02 = $betTimeArr[1];
        $openOLD = date("Y-m-d", strtotime($betTimeArr02[0][3]) - 86400);
        $autoAddTimeArr = array(180, 120, 60);
        foreach ($autoAddTimeArr as $autoAddTimeArrVal)
        {
            $autoLottoTimeQ = $autoLottoTime + $autoAddTimeArrVal;
            $startTime = date("Y-m-d H:i:00", $autoLottoTimeQ);
            if ($cnyDateSInt <= $autoLottoTimeQ && $autoLottoTimeQ <= $cnyDateEInt)
            {
                $this->autoLottoSetMsg("假日休市期间不开盘");
                return FALSE;
            }
            $arrIdx = array_search($startTime, $betTimeArr01);
            if ($arrIdx !== FALSE)
            {
                $sql = "!!SELECT `startbet`, `round` FROM {$this->table_name} WHERE `open` >= '{$openOLD}' AND `startbet` <= '{$startTime}' ORDER BY `startbet` DESC LIMIT 1";
                $result = $this->db->query($sql);
                $row = mysql_fetch_array($result);
                if ($row == FALSE)
                {
                    $this->autoLottoSetMsg("查无前日开盘时间,无法自动新增期数,今日期数请手动新增");
                    return FALSE;
                }
                if ($row['startbet'] == $startTime)
                {
                    return FALSE;
                }
                $arrIdxTemp = $arrIdx - 1;
                if (0 - 1 < $arrIdxTemp)
                {
                    if ($row['startbet'] != $betTimeArr01[$arrIdxTemp])
                    {
                        $this->autoLottoSetMsg("查无前期开盘时间,无法自动新增期数,请手动新增期数");
                        return FALSE;
                    }
                }
                $addData = array();
                $addData['round'] = $betTimeArr02[$arrIdx][0];
                $addData['open'] = $betTimeArr02[$arrIdx][3];
                $addData['openbet'] = $betTimeArr02[$arrIdx][2];
                $addData['startbet'] = $betTimeArr01[$arrIdx];
                $addData['stopbet'] = $betTimeArr02[$arrIdx][1];
                if ($lotto_id = $this->add($addData))
                {
                    $this->copyRateZeroToN($lotto_id);
                    if ($g_gametype_3d[$this->gametype]['type'] == 3)
                    {
                        $this->autoRateByTwoLong5H(array("id" => $lotto_id, "round" => $addData['round'], "open" => $addData['open']));
                    }
                    else
                    {
                        $this->autoRateByTwoLong3D(array("id" => $lotto_id, "round" => $addData['round'], "open" => $addData['open']));
                    }
                    if ($arrIdx < 1)
                    {
                        $openTempArr = explode("-", $addData['open']);
                        $this->createTblBettotal($openTempArr[0] . $openTempArr[1] . $openTempArr[2]);
                    }
                    $strTemp = array(substr($addData['startbet'], 0, 0 - 3), substr($betTimeArr02[$arrIdx][1], 0, 0 - 3), substr($betTimeArr02[$arrIdx][2], 0, 0 - 3));
                    $this->autoLottoSetMsg("自动新增期数::第{$betTimeArr02[$arrIdx][0]}期 ◎ {$strTemp[0]}开盘 ◎ {$strTemp[1]}关盘 ◎ {$strTemp[2]}开奖");
                    return TRUE;
                }
                else
                {
                    $this->autoLottoSetMsg("新增第{$betTimeArr02[$arrIdx][0]}期数资料时发生问题||开盘时间:" . $addData['startbet']);
                    return FALSE;
                }
            }
            else
            {
                continue;
            }
        }
        return FALSE;
    }

    public function autoLottoSetMsg($str = "")
    {
        if ($str != "")
        {
            $conf = new Config();
            $dt = date("Y-m-d H:i", systime());
            $value = "{$dt} @ " . htmlspecialchars($str, ENT_QUOTES);
            $conf->set("lotto_auto{$this->tbl}Msg", $value);
        }
    }

    public function autoRateByTwoLong3D($lottoData)
    {
        $chkBettypeArr = array("2_5" => 56, "2_4" => 59, "2_3" => 4, "2_2" => 5, "2_1" => 6, "1_5" => 57, "1_4" => 60, "1_3" => 7, "1_2" => 8, "1_1" => 9, "3_5" => 58, "3_4" => 61, "3_3" => 10, "3_2" => 11, "3_1" => 12);
        $conf = new Config();
        $RateConfig = new RateConfig_3d($this->gametype);
        $lotto_id = $lottoData['id'];
        $round = $lottoData['round'];
        $open = $lottoData['open'];
        $longautoadownVal = intval($conf->get("longautoadown_{$this->tbl}"));
        if ($longautoadownVal < 1)
        {
            return;
        }
        $subidStr = " AND subid = 0";
        $orderStr = "";
        $xyidArr = array();
        $twolongArr = two_long($this->gametype, FALSE, $longautoadownVal);

        $betname = judge_type($this->gametype);
        foreach ($twolongArr as $key => $val)
        {
            $keyArr = explode("_", $key);
            $keyTemp = "{$keyArr[0]}_{$keyArr[1]}";
            if (isset($chkBettypeArr[$keyTemp]))
            {
                $keyArr[0] = $chkBettypeArr[$keyTemp];
                $keyArr[1] = 0;
            }
            else
            {
                $keyArr = explode("_", $key);
            }
            $sql = "SELECT bettype_id, subid, downrate, limitrate, money FROM autorate_config_{$this->tbl} WHERE bettype_id = {$keyArr[0]} {$subidStr} LIMIT 1";
            $result = $this->db->query($sql);
            if ($row = mysql_fetch_array($result))
            {
                if ($row['money'] < 1)
                {
                    continue;
                }
                $numIdx = "r" . str_pad($keyArr[2], 2, "0", STR_PAD_LEFT);
                $xyidStr = "";
                $diskVal = 1;
                for (; $diskVal < 5; $diskVal++)
                {
                    $sqlRate = "!!SELECT * FROM rate_{$this->tbl} WHERE lotto_id IN ( {$lotto_id}, 0) AND bettype = {$keyArr[0]} AND subid = {$keyArr[1]} AND disk = {$diskVal} {$orderStr} LIMIT 2";
                    $result = $this->db->query($sqlRate);
                    $rateArr = array();
                    while ($rowR = mysql_fetch_array($result))
                    {
                        $lottoIdx = $rowR['lotto_id'];
                        $rateArr[$lottoIdx] = $rowR;
                    }
                    if (!isset($rateArr[0]))
                    {
                        $this->autoLottoSetMsg("查无基本赔率资料({$diskVal}盘), 自动二面长龙降倍失败");
                        continue;
                    }
                    if (!isset($rateArr[$lotto_id]))
                    {
                        $this->autoLottoSetMsg("查无第({$diskVal}盘){$round}期赔率资料, 自动二面长龙降倍失败");
                        continue;
                    }
                    $rateArrBase = $rateArr[0];
                    $rateArrOLD = $rateArr[$lotto_id];
                    $rateArrNEW = $rateArrOLD;
                    $rateOLD = $rateArrOLD[$numIdx];
                    $rateDown = ( $val - $longautoadownVal + 1 ) * $row['downrate'];
                    $rateNew = min(max($rateOLD - $rateDown, $row['limitrate']), $rateArrBase[$numIdx]);
                    $rateArrNEW[$numIdx] = $rateNew;
                    $sql = "UPDATE rate_{$this->tbl} SET {$numIdx} = {$rateNew}";
                    $sql .= " WHERE lotto_id = {$lotto_id} AND bettype = {$keyArr[0]} AND subid = {$keyArr[1]} AND disk = {$diskVal} LIMIT 1";
                    $result = $this->db->query($sql);
                    if ($diskVal == 1)
                    {
                        $valIdx = "{$keyArr[0]}_{$keyArr[1]}";
                        $fieldArr = array("method" => 8, "gametype" => $this->gametype, "round" => $round, "lotto_id" => $lotto_id, "betcontent" => "连{$val}期::[{$betname[$valIdx]}]" . $RateConfig->getBetContent($keyArr[0], $keyArr[2]), "orate" => $rateOLD, "nrate" => $rateNew, "pagename" => "3", "paperopen" => $open);
                        setRateRecord($fieldArr);
                    }
                }
            }
        }
    }

    public function autoRateByTwoLong5H($lottoData)
    {
        $conf = new Config();
        $lotto_id = $lottoData['id'];
        $round = $lottoData['round'];
        $open = $lottoData['open'];
        $longautoadownVal = intval($conf->get("longautoadown_{$this->tbl}"));
        if ($longautoadownVal < 1)
        {
            return;
        }
        $subidStr = " AND subid = 0";
        $orderStr = " ORDER BY xyid";
        $RateConfig = new RateConfig_5h($this->gametype);
        $xyidArr = $RateConfig->betXYArr;
        $twolongArr = two_long($this->gametype, FALSE, $longautoadownVal);
        $betname = judge_type($this->gametype);
        foreach ($twolongArr as $key => $val)
        {
            $keyArr = explode("_", $key);
            $sql = "SELECT bettype_id, subid, downrate, limitrate, money FROM autorate_config_{$this->tbl} WHERE bettype_id = {$keyArr[0]} {$subidStr} LIMIT 1";
            $result = $this->db->query($sql);
            if ($row = mysql_fetch_array($result))
            {
                if ($row['money'] < 1)
                {
                    continue;
                }
                $numStr = "r" . str_pad($keyArr[2], 2, "0", STR_PAD_LEFT);
                if (in_array($keyArr[0], $xyidArr))
                {
                    $xyidStr = " AND xyid = 1 ";
                }
                else
                {
                    $xyidStr = "";
                }
                $sqlRate = "!!SELECT {$numStr} FROM rate_{$this->tbl}  WHERE lotto_id = {$lotto_id} AND bettype = {$keyArr[0]} AND subid = {$keyArr[1]} AND disk = 1 {$xyidStr} {$orderStr} LIMIT 1";
                $result = $this->db->query($sqlRate);
                if ($rowR = mysql_fetch_array($result))
                {
                    $rateOLD = $rowR[0];
                    $rateDown = ( $val - $longautoadownVal + 1 ) * $row['downrate'];
                    $sql = "UPDATE rate_{$this->tbl} SET {$numStr} = IF(({$numStr} - {$rateDown}) < " . $row['limitrate'] . ", " . $row['limitrate'] . ", ({$numStr} - {$rateDown}))";
                    $sql .= " WHERE lotto_id = {$lotto_id} AND bettype = {$keyArr[0]} AND subid = {$keyArr[1]}";
                    $result = $this->db->query($sql);
                    $result = $this->db->query($sqlRate);
                    $rowR = mysql_fetch_array($result);
                    $rateNEW = $rowR[0];
                    $valIdx = "{$keyArr[0]}_{$keyArr[1]}";
                    $fieldArr = array("method" => 8, "gametype" => $this->gametype, "round" => $round, "lotto_id" => $lotto_id, "betcontent" => "连{$val}期::[{$betname[$valIdx]}]" . $RateConfig->getBetContent($keyArr[0], $keyArr[2]), "orate" => $rateOLD, "nrate" => $rateNEW, "pagename" => "3", "paperopen" => $open);
                    setRateRecord($fieldArr);
                }
            }
        }
    }

    public function checkBS($val, $flagVal = 4)
    {
        if ($flagVal < $val)
        {
            return 1;
        }
        else
        {
            return 2;
        }
    }

    public function checkSD($val)
    {
        if ($val == 0)
        {
            return 2;
        }
        else if ($val % 2 == 0)
        {
            return 2;
        }
        else
        {
            return 1;
        }
    }

    public function checkPC($val)
    {
        $reVal = 1;
        if ($val == 0)
        {
            return 2;
        }
        else if ($val == 1)
        {
            return 1;
        }
        else
        {
            $sqrtVal = sqrt($val);
            $i = 2;
            for (; $i <= $sqrtVal; $i++)
            {
                if ($val % $i == 0)
                {
                    $reVal = 2;
                    break;
                }
            }
            return $reVal;
        }
    }

    public function checkPCSD($val)
    {
        $pc = $this->checkPC($val);
        $sd = $this->checkSD($val);
        if ($pc == 1 && $sd == 1)
        {
            return 1;
        }
        else if ($pc == 1 && $sd == 2)
        {
            return 2;
        }
        else if ($pc == 2 && $sd == 1)
        {
            return 3;
        }
        else if ($pc == 2 && $sd == 2)
        {
            return 4;
        }
    }

    public function checkPCBS($val, $flagVal = 4)
    {
        $pc = $this->checkPC($val);
        $bs = $this->checkBS($val, $flagVal);
        if ($pc == 1 && $bs == 1)
        {
            return 1;
        }
        else if ($pc == 1 && $bs == 2)
        {
            return 2;
        }
        else if ($pc == 2 && $bs == 1)
        {
            return 3;
        }
        else if ($pc == 2 && $bs == 2)
        {
            return 4;
        }
    }

    public function checkG3($n3, $n2, $n1)
    {
        if ($n3 == $n2 && $n2 == $n1)
        {
            return 0;
        }
        else if ($n3 != $n2 && $n2 != $n1 && $n3 != $n1)
        {
            return 0;
        }
        else
        {
            return 1;
        }
    }

    public function checkG6($n3, $n2, $n1)
    {
        if ($n3 != $n2 && $n2 != $n1 && $n3 != $n1)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }

    public function checkBsubS($n3, $n2, $n1)
    {
        $tempArr = array($n3, $n2, $n1);
        sort($tempArr);
        return $tempArr[2] - $tempArr[0];
    }

    public function checkRO($n3, $n2, $n1)
    {
        $otherCheckTemp = 0;
        $checkArr = array("012", "123", "234", "345", "456", "567", "678", "789", "089", "019");
        $tempArr = array($n3, $n2, $n1);
        sort($tempArr);
        $numArrTemp = implode("", $tempArr);
        $checkArr02 = array($tempArr[1] - $tempArr[0], $tempArr[2] - $tempArr[0], $tempArr[2] - $tempArr[1]);
        if ($n1 == $n2 && $n1 == $n3 && $n2 == $n3)
        {
            $otherCheckTemp = 48;
        }
        else if (in_array($numArrTemp, $checkArr))
        {
            $otherCheckTemp = 49;
        }
        else if ($n1 == $n2 || $n1 == $n3 || $n2 == $n3)
        {
            $otherCheckTemp = 50;
        }
        else if (in_array(1, $checkArr02) || in_array(9, $checkArr02))
        {
            $otherCheckTemp = 51;
        }
        else
        {
            $otherCheckTemp = 52;
        }
        return $otherCheckTemp;
    }

    public function checkDbnum($n3, $n2, $n1)
    {
        if ($n1 == $n2 && $n1 == $n3 && $n2 == $n3)
        {
            return 0 - 1;
        }
        else if ($n1 == $n2)
        {
            return $n1;
        }
        else if ($n1 == $n3)
        {
            return $n1;
        }
        else if ($n2 == $n3)
        {
            return $n2;
        }
        else
        {
            return 0 - 1;
        }
    }

    public function checkOpen($n3, $n2, $n1, $n4 = -1, $n5 = -1)
    {
        if (0 - 1 < $n3 && 0 - 1 < $n2 && 0 - 1 < $n1)
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }

    public function checkOpenStart($n3, $n2, $n1)
    {
        if ($n3 == "" && $n2 == "" && $n1 == "")
        {
            return FALSE;
        }
        else if (0 - 1 < $n3 || 0 - 1 < $n2 || 0 - 1 < $n1)
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }

    public function checkNum($n)
    {
        if ($n == "")
        {
            return TRUE;
        }
        else if (0 - 1 <= $n && $n < 10)
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }
	function CountNN($arr, $size = 3) {
		$len = count($arr);
		$max = pow(2,$len) - pow(2,$len-$size);
		$min = pow(2,$size)-1;
		
		$r_arr = array();
		for ($i=$min; $i<=$max; $i++){
			$count =0;
			$t_arr = array();
			for ($j=0; $j<$len; $j++){
				$a = pow(2, $j);
				$t = $i&$a;
				if($t == $a){
					$t_arr[] = $arr[$j];
					$count++;
				}
			}
			if($count == $size){
				$v=$t_arr[0]+$t_arr[1]+$t_arr[2];
				if($v%10==0)
					return $t_arr;
			}
		}
		return $r_arr;
	}
	function CountNNResult($arr1)
	{
		if(($arr1[0]+$arr1[1]+$arr1[2]+$arr1[3]+$arr1[4])==0) return 10;
		$arr2=$this->CountNN($arr1);
		foreach($arr2 as $v)
		{
			foreach($arr1 as $keys=>$v2)
			{
				if($v2==$v)
				{
					unset($arr1[$keys]);
					break;	
				}		
			}		
		}
		if(count($arr1)==2)
		{
			$c=0;
			foreach($arr1 as $v2)
				$c+=$v2;
			return $c%10;	
		}
		else
			return 12;	
	}
	function CountNNDX($v)
	{
		if($v!=12)
			return ($v>=6)?1:2; //大:小		
		else
			return 0;//都不中	
	}
	function CountNNDS($v)
	{
		if($v!=12)
			return ($v%2)?1:2; //单:双		
		else
			return 0;//都不中	
	}
	function CountSH($arr){
		$result=array();
		$arr1=array_count_values($arr);
		//两对
		if(count($arr1)==3)
		{
			foreach($arr1 as $v)
			{
				if($v==2) 
				{
					$result[]=5;	
					break;
				}
			}		
		}else{
		//一对
			foreach($arr1 as $v)
			{
				if($v==2) 
				{
					$result[]=6;	
					break;
				}
			}
		}
		//葫芦
		if(count($arr1)==2){
			foreach($arr1 as $v)
			{
				if($v==2 || $v==3) 
				{
					$result[]=2;
					break;
				}
			}	
		}
		//顺子
		$dz=true;
		for($i=0;$i<4;$i++)
		{
			if($arr[$i]==9 && $arr[$i+1]!=0)
			{
				$dz=false;
				break;
			}
			elseif($arr[$i]!=9 && $arr[$i+1]!=$arr[$i]+1)
			{
				$dz=false;
				break;
			}
		}
		if($dz) $result[]=3;
		foreach($arr1 as $key=>$val)
		{
			switch($val)
			{
				case 5:
					$result[]=0;
					break;
				case 4:
					$result[]=1;
					break;
				case 3:
					$result[]=4;
					break;		
			}
		}	
		if(count($result)==0)
			$result[]=7;	
		return join(",",$result);
	}
    public function showText($n, $type)
    {
        $bsArr = array(0 => "", 1 => "<font class=text01>大</font>", 2 => "<font class=text02>小</font>");
        $sdArr = array(0 => "", 1 => "<font class=text01>单</font>", 2 => "<font class=text02>双</font>");
        $pcArr = array(0 => "", 1 => "<font class=text01>质</font>", 2 => "<font class=text02>合</font>");
        $pcsdArr = array(0 => "", 1 => "<font class=text01>质单</font>", 2 => "<font class=text02>质双</font>", 3 => "<font class=text03>合单</font>", 4 => "<font class=text04>合双</font>");
        $pcbsArr = array(0 => "", 1 => "<font class=text01>质大</font>", 2 => "<font class=text02>质小</font>", 3 => "<font class=text03>合大</font>", 4 => "<font class=text04>合小</font>");
        $animalArr = array(1 => "<font style=\"color:black;\">龙</font>", 2 => "<font style=\"color:red;\">虎</font>", 3 => "<font style=\"color:saddlebrown;\">和</font>");
		$reVal = "";
        switch ($type)
        {
            case "sd" :
                $reVal = $sdArr[$n];
                break;
            case "bs" :
                $reVal = $bsArr[$n];
                break;
            case "pcsd" :
                $reVal = $pcsdArr[$n];
                break;
            case "pcbs" :
                $reVal = $pcbsArr[$n];
                break;
            case "pc" :
                $reVal = $pcArr[$n];
                break;
            case "animal" :
                $reVal = $animalArr[$n];
                break;
			case 'nn':
				$tempArr = array("牛牛","牛1","牛2","牛3","牛4","牛5","牛6","牛7","牛8","牛9","牛牛","无牛","无牛","无牛","无牛","无牛");
				$reVal = $tempArr[$n];
				break;
			case 'nn2':
				$tempArr = array("牛1","牛2","牛3","牛4","牛5","牛6","牛7","牛8","牛9","牛牛","牛大","牛小","无牛","牛单","牛双");
				$reVal = $tempArr[$n-1];
				break;
			case 'sh':
				$tempArr = array("五条","四条","葫芦","顺子","三条","两对","单对","散号");
				$m=min(explode(',',$n));
				$reVal = $tempArr[$m[0]];
				break;
			case 'qt':
				$tempArr = array("豹子","顺子","对子","半顺","杂六");
				$reVal = $tempArr[$n];
				break;
			case 'other':
				//$tempArr = array(48=>"豹子",49=>"顺子",50=>"对子",51=>"半顺",52=>"杂六");
				$tempArr = array("豹子","顺子","对子","半顺","杂六");
				$reVal = $tempArr[$n];
				break;
            default :
                $reVal = $n;
                break;
        }
        return $reVal;
    }

    public function calOpenNum($n3, $n2, $n1, $n4 = -1, $n5 = -1)
    {
        $updateData = array();
        if ($this->checkOpen($n3, $n2, $n1, $n4, $n5))
        {
            $nArr = array($n1, $n2, $n3);
            sort($nArr);
            $updateData['p3bs'] = $this->checkBS($n3, 4);
            $updateData['p2bs'] = $this->checkBS($n2, 4);
            $updateData['p1bs'] = $this->checkBS($n1, 4);
            $updateData['p3sd'] = $this->checkSD($n3);
            $updateData['p2sd'] = $this->checkSD($n2);
            $updateData['p1sd'] = $this->checkSD($n1);
            $updateData['p3pc'] = $this->checkPC($n3);
            $updateData['p2pc'] = $this->checkPC($n2);
            $updateData['p1pc'] = $this->checkPC($n1);
            $updateData['r2'] = $nArr[0] . $nArr[1] . $nArr[0] . $nArr[2] . $nArr[1] . $nArr[2];
            $updateData['r2p0sd'] = $this->checkSD($n3 + $n2);
            $updateData['r2p1sd'] = $this->checkSD($n3 + $n1);
            $updateData['r2p2sd'] = $this->checkSD($n2 + $n1);
            $updateData['p3'] = $n3 . $n2 . $n1;
            $updateData['r3'] = $nArr[0] . $nArr[1] . $nArr[2];
            $updateData['p321sd'] = $this->checkSD($n3 + $n2 + $n1);
            $updateData['p321bs'] = $this->checkBS($n3 + $n2 + $n1, 13);
            $updateData['rbs'] = $this->checkBsubS($n3, $n2, $n1);
            $chkNum = $n3 + $n2;
            if ($chkNum < 5)
            {
                $updateData['r2ap32'] = 4;
            }
            else if (13 < $chkNum)
            {
                $updateData['r2ap32'] = 14;
            }
            else
            {
                $updateData['r2ap32'] = $chkNum;
            }
            $chkNum = $n3 + $n1;
            if ($chkNum < 5)
            {
                $updateData['r2ap31'] = 4;
            }
            else if (13 < $chkNum)
            {
                $updateData['r2ap31'] = 14;
            }
            else
            {
                $updateData['r2ap31'] = $chkNum;
            }
            $chkNum = $n2 + $n1;
            if ($chkNum < 5)
            {
                $updateData['r2ap21'] = 4;
            }
            else if (13 < $chkNum)
            {
                $updateData['r2ap21'] = 14;
            }
            else
            {
                $updateData['r2ap21'] = $chkNum;
            }
            $updateData['r2ap32m'] = ( $n3 + $n2 ) % 10;
            $updateData['r2ap31m'] = ( $n3 + $n1 ) % 10;
            $updateData['r2ap21m'] = ( $n2 + $n1 ) % 10;
            $chkNum = $n3 + $n2 + $n1;
            if ($chkNum < 7)
            {
                $updateData['r3a'] = 6;
            }
            else if (20 < $chkNum)
            {
                $updateData['r3a'] = 21;
            }
            else
            {
                $updateData['r3a'] = $chkNum;
            }
            $updateData['r3am'] = ( $n3 + $n2 + $n1 ) % 10;
            $updateData['r3abs'] = $this->checkBS($updateData['r3am'], 4);
            $chkArrTemp = array(1, 2, 3, 5, 7);
            $chkNumTemp2 = ( $n3 + $n2 + $n1 ) % 10;
            $updateData['r3apc'] = in_array($chkNumTemp2, $chkArrTemp) ? 1 : 2;
            $updateData['ro'] = $this->checkRO($n3, $n2, $n1);
            $updateData['dbnum'] = $this->checkDbnum($n3, $n2, $n1);
        }
        return $updateData;
    }

}

class MyLotto_3d_5n extends MyLotto_3d
{

    public function checkOpen($n3, $n2, $n1, $n4, $n5)
    {
        $chkArr = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
        if (isset($chkArr[$n5], $chkArr[$n4]) && isset($chkArr[$n3]) && isset($chkArr[$n2]) && isset($chkArr[$n1]))
        {
            return true;
        }
        return false;
    }

    public function calOpenNum($n3, $n2, $n1, $n4, $n5)
    {
        $updateData = array();
        if ($this->checkOpen($n3, $n2, $n1, $n4, $n5))
        {
            $nArr = array($n1, $n2, $n3, $n4, $n5);
            sort($nArr);
            $updateData['p5bs'] = $this->checkBS($n5, 4);
            $updateData['p4bs'] = $this->checkBS($n4, 4);
            $updateData['p3bs'] = $this->checkBS($n3, 4);
            $updateData['p2bs'] = $this->checkBS($n2, 4);
            $updateData['p1bs'] = $this->checkBS($n1, 4);
            $updateData['p5sd'] = $this->checkSD($n5);
            $updateData['p4sd'] = $this->checkSD($n4);
            $updateData['p3sd'] = $this->checkSD($n3);
            $updateData['p2sd'] = $this->checkSD($n2);
            $updateData['p1sd'] = $this->checkSD($n1);
            $updateData['p5pc'] = $this->checkPC($n5);
            $updateData['p4pc'] = $this->checkPC($n4);
            $updateData['p3pc'] = $this->checkPC($n3);
            $updateData['p2pc'] = $this->checkPC($n2);
            $updateData['p1pc'] = $this->checkPC($n1);
            $nArr_sub2 = array($n5, $n4, $n3);
            sort($nArr_sub2);
            $nArr_sub1 = array($n4, $n3, $n2);
            sort($nArr_sub1);
            $nArr_sub0 = array($n3, $n2, $n1);
            sort($nArr_sub0);
            $updateData['r2_sub2'] = $nArr_sub2[0] . $nArr_sub2[1] . $nArr_sub2[0] . $nArr_sub2[2] . $nArr_sub2[1] . $nArr_sub2[2];
            $updateData['r2_sub1'] = $nArr_sub1[0] . $nArr_sub1[1] . $nArr_sub1[0] . $nArr_sub1[2] . $nArr_sub1[1] . $nArr_sub1[2];
            $updateData['r2'] = $nArr_sub0[0] . $nArr_sub0[1] . $nArr_sub0[0] . $nArr_sub0[2] . $nArr_sub0[1] . $nArr_sub0[2];
            $updateData['r2a_p54sd'] = $this->checkSD($n5 + $n4);
            $updateData['r2a_p53sd'] = $this->checkSD($n5 + $n3);
            $updateData['r2a_p52sd'] = $this->checkSD($n5 + $n2);
            $updateData['r2a_p51sd'] = $this->checkSD($n5 + $n1);
            $updateData['r2a_p43sd'] = $this->checkSD($n4 + $n3);
            $updateData['r2a_p42sd'] = $this->checkSD($n4 + $n2);
            $updateData['r2a_p41sd'] = $this->checkSD($n4 + $n1);
            $updateData['r2p0sd'] = $this->checkSD($n3 + $n2);
            $updateData['r2p1sd'] = $this->checkSD($n3 + $n1);
            $updateData['r2p2sd'] = $this->checkSD($n2 + $n1);
            $updateData['p3'] = $n3 . $n2 . $n1;
            $updateData['r3_sub2'] = $nArr_sub2[0] . $nArr_sub2[1] . $nArr_sub2[2];
            $updateData['r3_sub1'] = $nArr_sub1[0] . $nArr_sub1[1] . $nArr_sub1[2];
            $updateData['r3'] = $nArr_sub0[0] . $nArr_sub0[1] . $nArr_sub0[2];
            $updateData['p321sd'] = $this->checkSD($n3 + $n2 + $n1);
            $updateData['p321bs'] = $this->checkBS($n3 + $n2 + $n1, 13);
            $updateData['rbs_sub2'] = $this->checkBsubS($n5, $n4, $n3);
            $updateData['rbs_sub1'] = $this->checkBsubS($n4, $n3, $n2);
            $updateData['rbs'] = $this->checkBsubS($n3, $n2, $n1);
            $chkNum = $n3 + $n2;
            if ($chkNum < 5)
            {
                $updateData['r2ap32'] = 4;
            }
            else if (13 < $chkNum)
            {
                $updateData['r2ap32'] = 14;
            }
            else
            {
                $updateData['r2ap32'] = $chkNum;
            }
            $chkNum = $n3 + $n1;
            if ($chkNum < 5)
            {
                $updateData['r2ap31'] = 4;
            }
            else if (13 < $chkNum)
            {
                $updateData['r2ap31'] = 14;
            }
            else
            {
                $updateData['r2ap31'] = $chkNum;
            }
            $chkNum = $n2 + $n1;
            if ($chkNum < 5)
            {
                $updateData['r2ap21'] = 4;
            }
            else if (13 < $chkNum)
            {
                $updateData['r2ap21'] = 14;
            }
            else
            {
                $updateData['r2ap21'] = $chkNum;
            }
            $updateData['r2ap32m'] = ( $n3 + $n2 ) % 10;
            $updateData['r2ap31m'] = ( $n3 + $n1 ) % 10;
            $updateData['r2ap21m'] = ( $n2 + $n1 ) % 10;
            $chkNum = $n3 + $n2 + $n1;
            if ($chkNum < 7)
            {
                $updateData['r3a'] = 6;
            }
            else if (20 < $chkNum)
            {
                $updateData['r3a'] = 21;
            }
            else
            {
                $updateData['r3a'] = $chkNum;
            }
            $updateData['r3am'] = ( $n3 + $n2 + $n1 ) % 10;
            $updateData['r3abs'] = $this->checkBS($updateData['r3am'], 4);
            $chkArrTemp = array(1, 2, 3, 5, 7);
            $chkNumTemp2 = ( $n3 + $n2 + $n1 ) % 10;
            $updateData['r3apc'] = in_array($chkNumTemp2, $chkArrTemp) ? 1 : 2;
            $updateData['ro'] = $this->checkRO($n3, $n2, $n1);
            $updateData['dbnum'] = $this->checkDbnum($n3, $n2, $n1);
			$updateData['nn'] = $this->CountNNResult(array($n1,$n2,$n3,$n4,$n5));
			$updateData['nnsd'] = $this->CountNNDS($updateData['nn']);
			$updateData['nnbs'] = $this->CountNNDX($updateData['nn']);
			$updateData['sh'] = $this->CountSH(array($n5,$n4,$n3,$n2,$n1));
        }
        return $updateData;
    }

}

?>